﻿Public Class AlbumList
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    
    Protected Sub btn_Add_Addalbum_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_Add_Addalbum.Click
        Dim T As Integer = tag(txt_add_alb_name.Text)
        If Fup_add_Albpicture.HasFile Then
            Fup_add_Albpicture.SaveAs(Server.MapPath("Pictures" & "/" & Fup_add_Albpicture.FileName))
        End If
        Dim sql As String = " INSERT INTO Albums (Cover_Picture, Album_Name, Release_year, Description,Tag_ID) VALUES (@a1,@a2,@a3,@a4,@a5)"
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        cmd.Parameters.AddWithValue("@a1", "Pictures/" & Fup_add_Albpicture.FileName)
        cmd.Parameters.AddWithValue("@a2", txt_add_alb_name.Text)
        cmd.Parameters.AddWithValue("@a3", txt_add_release_year.Text)
        cmd.Parameters.AddWithValue("@a4", txt_add_Alb_Desc.Text)
        cmd.Parameters.AddWithValue("@a5", T)
        conn.Open()
        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Response.Redirect("List.aspx")
    End Sub
    Protected Function tag(ByVal S As String) As Integer
        Dim _tagID As Integer
        Dim conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("SingerProfileConStr").ConnectionString)
        conn.Open()
        Dim sql As String = " Select ID from AlbumsWithTags where Tag = MID('" & txt_add_alb_name.Text & "',1,1)"
        Dim cmd As OleDb.OleDbCommand = New OleDb.OleDbCommand(sql, conn)
        Dim Dreader As OleDb.OleDbDataReader

        Dreader = cmd.ExecuteReader()
        Dreader.Read()
        _tagID = CDbl(Dreader("ID"))

        conn.Close()
        cmd.Dispose()
        conn.Dispose()
        Return _tagID
    End Function

End Class